package com.ruoyi.project.explorer.controller;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.explorer.domain.ExplorAddParam;
import com.ruoyi.project.explorer.domain.Explorer;
import com.ruoyi.project.explorer.domain.ExplorerDto;
import com.ruoyi.project.explorer.service.IExplorerService;
import com.ruoyi.project.jiaowu.totalcoursemain.domain.TbTotalCourseMain;
import com.ruoyi.project.jiaowu.totalcoursemain.mapper.TbTotalCourseMainMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.tomcat.jni.Global;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 资源管理Controller
 * 
 * @author ruoyi
 * @date 2019-11-29
 */
@Controller
@RequestMapping("/system/explorer")
public class ExplorerController extends BaseController
{
    private String prefix = "system/explorer";

    @Autowired
    private IExplorerService explorerService;
    @Autowired
    private TbTotalCourseMainMapper tbTotalCourseMainMapper;

    @RequiresPermissions("system:explorer:view")
    @GetMapping()
    public String explorer()
    {
        return prefix + "/explorer";
    }

    /**
     * 查询资源管理列表
     */
    @RequiresPermissions("system:explorer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Explorer explorer,Model model)
    {
        startPage();
        List<ExplorerDto> list = explorerService.selectExplorerList(explorer);
        return getDataTable(list);
    }

    /**
     * 导出资源管理列表
     */
    @RequiresPermissions("system:explorer:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Explorer explorer)
    {
        List<ExplorerDto> list = explorerService.selectExplorerList(explorer);
        ExcelUtil<ExplorerDto> util = new ExcelUtil<ExplorerDto>(ExplorerDto.class);
        return util.exportExcel(list, "explorer");
    }

    /**
     * 新增资源管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存资源管理
     */
    @RequiresPermissions("system:explorer:add")
    @Log(title = "资源管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ExplorAddParam explorAddParam)
    {
        return explorerService.insertExplorer(explorAddParam);
    }

    /**
     * 修改资源管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Explorer explorer = explorerService.selectExplorerById(id);
        mmap.put("explorer", explorer);
        return prefix + "/edit";
    }

    /**
     * 修改保存资源管理
     */
    @RequiresPermissions("system:explorer:edit")
    @Log(title = "资源管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Explorer explorer)
    {
        //System.err.println(explorer);
        //return toAjax(true);
        return toAjax(explorerService.updateExplorer(explorer));
    }

    /**
     * 删除资源管理
     */
    @RequiresPermissions("system:explorer:remove")
    @Log(title = "资源管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(explorerService.deleteExplorerByIds(ids));
    }



}
